package com.cn.customer.housekeeping.housekeepingc.ui;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cn.customer.housekeeping.housekeepingc.R;
import com.cn.customer.housekeeping.housekeepingc.bean.BaseResp;
import com.cn.customer.housekeeping.housekeepingc.biz.personcenter.IUserLoginView;
import com.cn.customer.housekeeping.housekeepingc.constant.Constants;
import com.cn.customer.housekeeping.housekeepingc.ui.base.BaseActivity;


public class WebviewActivity extends BaseActivity implements IUserLoginView {
    private String webViewUrl, webViewTitle;
    private WebView mVWv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_webview);
        webViewUrl = getIntent().getStringExtra(Constants.WEBVIEW_URL);
        webViewTitle = getIntent().getStringExtra(Constants.WEBVIEW_TITLE);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initViews() {
        mVWv = (WebView) findViewById(R.id.v_wv);
    }

    @Override
    public void initListeners() {
        initWebViewSettings();
    }

    @Override
    public void initData() {
        loadCookieUrl(webViewUrl);
    }


    public void loadCookieUrl(String webViewUrl) {
        if (!TextUtils.isEmpty(webViewUrl)) {
            if (webViewUrl.startsWith("http://") || webViewUrl.startsWith("https://")) {
                mVWv.loadUrl(webViewUrl);
            }
        }
    }

    private void initWebViewSettings() {
        WebSettings mWebSettings = mVWv.getSettings();
        mWebSettings.setAllowFileAccess(true);
        mWebSettings.setAllowFileAccess(true);
        mWebSettings.setAppCacheEnabled(true);
        mWebSettings.setDomStorageEnabled(true);
        mWebSettings.setDatabaseEnabled(true);
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setAppCacheMaxSize(1024 * 1024 * 8);
        mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebSettings.setPluginState(WebSettings.PluginState.ON);
        mWebSettings.setUseWideViewPort(true);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setBuiltInZoomControls(true);
        mWebSettings.setSupportZoom(true);
        mWebSettings.setAllowFileAccess(false);
        mWebSettings.setAppCacheEnabled(false);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        mWebSettings.setAppCachePath(appCachePath);
        mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mVWv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

            }


            @TargetApi(android.os.Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                onReceivedError(view, error.getErrorCode(), error.getDescription().toString(), request.getUrl().toString());
            }


            @SuppressWarnings("deprecation")
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                //用javascript隐藏系统定义的404页面信息
//                String data = "";
//                view.loadUrl("javascript:document.body.innerHTML=\"" + data + "\"");
            }

        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mVWv.canGoBack()) {
            mVWv.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void setHeader() {
        super.setHeader();
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle.setText(webViewTitle);
        tvTopRighttitle.setVisibility(View.GONE);
    }

    @Override
    public void onError(BaseResp s) {

    }

    @Override
    public void onSuccess(BaseResp s) {

    }
}
